package com.shine.minepressure.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.shine.minepressure.R;
import com.shine.minepressure.utils.ScreenUtils;

public class MineBgView extends View {

    private Bitmap bitmapBg;
    private int screenWidth, screenHeight;
    private int bitmapWidth, bitmapHeight;
    private Context mContext;
    private int viewWidth;
    private int viewHeight;

    public MineBgView(Context context) {
        super(context);
        mContext = context;
    }

    public MineBgView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MineBgView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        bitmapBg = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_mine_bg);

        bitmapWidth = bitmapBg.getWidth();
        bitmapHeight = bitmapBg.getHeight();
        //img 宽967 高 575
        //bitmapWidth=1330
        //bitmapHeight=791
        //screenWidth=1080
        //screenHeight=2088

        screenWidth = ScreenUtils.getScreenWidth(mContext);
        screenHeight = ScreenUtils.getScreenHeight(mContext);

//        int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);// 获得控件的宽度
//        int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);//获得控件的高度

        viewWidth = screenWidth;
        viewHeight = screenWidth * bitmapHeight / bitmapWidth;
        setMeasuredDimension(screenWidth, viewHeight);//设置宽和高
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制图片
        Paint bgPaint = new Paint();
//        canvas.drawBitmap(bitmapBg, 0, 0, bgPaint);
        canvas.drawBitmap(bitmapBg, null, new Rect(0, 0, viewWidth, viewHeight), bgPaint);

        //直线起点  234,231
        //中点  652,231
        //  642,296

        Paint p = new Paint();
        p.setColor(Color.RED);// 设置红色
//        canvas.drawText("画圆：", 10, 20, p);// 画文本
        canvas.drawCircle(pointX(234), pointY(231), 10, p);// 小圆
//        canvas.drawCircle(10, 10, 10, p);// 小圆

        Paint linePaint = new Paint();
        linePaint.setStrokeWidth(8);
        linePaint.setColor(Color.GREEN);
        canvas.drawLine(pointX(234), pointY(231), pointX(652), pointY(231), linePaint);
        linePaint.setColor(Color.YELLOW);
        canvas.drawLine(pointX(652), pointY(231), pointX(642), pointY(296), linePaint);

        p.setColor(Color.GRAY);
        canvas.drawCircle(pointX(642), pointY(296), 10, p);// 小圆

    }

    private float pointX(float point) {
        return point / 967 * viewWidth;
    }

    private float pointY(float point) {
        return point / 575 * viewHeight;
    }
}